# -*- coding: UTF-8 -*-

from google.appengine.ext import webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 
from google.appengine.api.urlfetch import fetch
import httplib

hostname = "api.foursquare.com"

class FSAPI(webapp.RequestHandler): 
    def post(self):
        self.Handle("POST")
        
    def get(self):
        self.Handle("GET")
        
    def Handle(self, method):
        try:
            headers = {}
            for header in self.request.headers:
                headers[header] = self.request.headers[header]
            body = self.request.body
            path = self.request.path
            query = self.request.query_string
            if query != None:
                url = "%s?%s" % (path, query)
            else:
                url = path
            h = httplib.HTTPConnection(hostname)
            h.request(method, url, body, headers)
            resp = h.getresponse()
            h.close()
            self.response.set_status(resp.status)
            self.response.out.write(resp.read())
        except:
            self.response.set_status(500)
            self.response.out.write("Server Error")
            
application = webapp.WSGIApplication(
                                     [('/.*', FSAPI)], 
                                     debug=True) 
 
 
 
def main(): 
    run_wsgi_app(application) 
 
if __name__ == "__main__": 
    main()